package com.qzxy.premiumhotelsystem.controller.admin;


import com.qzxy.premiumhotelsystem.common.constant.VipStatusConstant;
import com.qzxy.premiumhotelsystem.domain.dto.VipOrderPageDTO;
import com.qzxy.premiumhotelsystem.domain.dto.VipPageDTO;
import com.qzxy.premiumhotelsystem.domain.entity.Vip;
import com.qzxy.premiumhotelsystem.domain.entity.VipTypes;
import com.qzxy.premiumhotelsystem.common.result.PageResult;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.common.result.ResultCodeEnum;
import com.qzxy.premiumhotelsystem.service.VipOrdersService;
import com.qzxy.premiumhotelsystem.service.VipService;
import com.qzxy.premiumhotelsystem.service.VipTypesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: VipController
 * Package: com.qzxy.premiumhotelsystem.controller
 * Description:
 *
 * @Author Qwh
 * @Create 2024/9/12 16:04
 * @Version 1.0
 */

@RestController("adminVipController")
@RequestMapping("/admin/vip")
@Tag(name = "管理端vip会员相关接口")
@Slf4j
public class VipController {

    @Resource
    private VipOrdersService vipOrdersService;

    @Resource
    private VipTypesService vipTypesService;

    @Resource
    private VipService vipService;


    @Operation(summary = "根据条件分页查询所有会员订单")
    @GetMapping("/orders/list")
    public Result<PageResult> listVipOrders(@ParameterObject @Validated VipOrderPageDTO vipOrderPageDTO) {
        log.info("管理端分页查询vip订单记录，VipOrderPageDTO：{}", vipOrderPageDTO);
        Result<PageResult> result = vipOrdersService.pageVipOrderVos(vipOrderPageDTO);
        return result;
    }

    @Operation(summary = "根据条件分页查询所有会员记录")
    @GetMapping("/list")
    public Result<PageResult> listVips(@ParameterObject @Validated VipPageDTO vipPageDTO) {
        log.info("管理端分页查询会员记录，VipPageDTO：{}", vipPageDTO);
        Result<PageResult> result = vipOrdersService.pageVipVos(vipPageDTO);
        return result;
    }

    @Operation(summary = "新增会员类型", description = "要写得全部信息都填完整，不然增加此会员类型就没有意义")
    @PostMapping("/types")
    public Result addVipType(@RequestBody VipTypes vipTypes) {
        try {
//            int size = vipTypesService.list().size();
//            vipTypes.setVipTypeId(size+1);
            vipTypesService.save(vipTypes);
        } catch (DuplicateKeyException e) {
            throw new RuntimeException("vip_type唯一索引异常");
        }
        return Result.ok("新增会员类型成功");
    }

    @Operation(summary = "修改会员类型")
    @PutMapping("/types")
    public Result updateVipType(@Validated @RequestBody VipTypes vipTypes) {
        try {
            vipTypesService.updateById(vipTypes);
        } catch (DuplicateKeyException e) {
            throw new RuntimeException("vip_type唯一索引异常");
        }
        return Result.ok("修改成功");
    }

    @Operation(summary = "删除会员类型")
    @DeleteMapping("/types/{id}")
    public Result deleteVipType(@PathVariable Integer id) {
        boolean removed = vipTypesService.removeById(id);
        if (!removed) {
            return Result.build("删除失败，房间类型不存在", ResultCodeEnum.SERVER_ERROR);
        }
        return Result.ok("删除房间类型成功");
    }

    @Operation(summary = "批量更新会员状态", description = "id填的是vip表的主键id,如 6,7 用英文的,隔开 且不要[ ] statusNumber为1-有效 2-失效")
    @PutMapping("/status/update")
    public Result updateVipStatus(@RequestParam("ids") List<Integer> ids, @RequestParam("statusNumber") Integer statusNumber) {
        log.info("批量更新会员状态，ids：{}，状态编号：{}", ids, statusNumber);

        // 根据状态编号获取对应的状态描述
        String status = VipStatusConstant.getStatusDescription(statusNumber);

        List<Vip> vipList = new ArrayList<>();
        for (Integer id : ids) {
            Vip vip = Vip.builder().id(id)
                    .status(status).build();
            vipList.add(vip);
        }
        vipService.updateBatchById(vipList);

        return Result.ok("成功更新会员状态");
    }

    @Operation(summary = "所有会员类型")
    @GetMapping("/type/list")
    public Result typelListVips() {
        return Result.ok(vipTypesService.list());
    }

}
